#include "music.hpp"

#include <iostream>

Music::Music()
{
	mus = NULL;
}

Music::~Music()
{
}

#include "audio.hpp"
#include "disk.hpp"

bool Music::Open(std::string & name)
{
	return Open(name.c_str());
}

bool Music::Open(const char *name)
{
	const char *DIR = "snd/music/";
	const char *EXT = ".ogg";

	std::string filename (DIR);
	filename += name;
	filename += EXT;

	char *text;
	int readct = Dsk.CStrOpen(filename, &text);
	if (readct <= 0)
		return false;

	char *muserr = NULL;

	SDL_RWops *rwo = SDL_RWFromMem(text, readct);
	if (!rwo)
		muserr = SDL_GetError();

	if (!muserr)
		mus = Mix_LoadMUS_RW(rwo);

	if (!mus)
		muserr = Mix_GetError();

	if (muserr)
	{
		std::cerr << "Music::Open(" << name << "): " << muserr;
		std::cerr << std::endl;
		return false;
	}
	
	return true;
}

bool Music::Opened()
{
	return mus != NULL;
}

void Music::Close()
{
	if (mus)
		Mix_FreeMusic(mus);
	mus = NULL;
}

void Music::Play(int track)
{
	Mix_PlayMusic(mus, -1);
}

void Music::Resume()
{
	Mix_ResumeMusic();
}

void Music::Stop()
{
	Mix_PauseMusic();
}

void Music::Fade(int time)
{
	Mix_FadeOutMusic(time);
}

Music Mus;
